home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8023 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: isonews.bbn.hp.com!hpbblb!news
  2. From: Matthias Dittrich <matti>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple Program Question
  5. Date: 28 Feb 1996 07:32:29 GMT
  6. Organization: Hewlett-Packard Co.
  7. Message-ID: <4h10ed$5es@hpbblb.bbn.hp.com>
  8. References: <4gsr9u$sk6@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: trabant.bbn.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
  14. X-URL: news:4gsr9u$sk6@newsbf02.news.aol.com
  15.  
  16. tycope@aol.com (Tycope) wrote:
  17. >I am trying to write a non -interactive program that calculates all
  18. >integer triples (i, j, k) such that 
  19. >0 < i < j < k < l and i + j + k = l. Print out the number of triples that
  20. >satisfy the requirements and print out every millionth triple.  Can anyone
  21. >see where I am missing the boat in the following code.  The program runs
  22. >and immediately terminates.  Thanks in advance for any feedback. 
  23. >
  24. >#include <stdio.h>
  25. >
  26. >long int i, j, k, l;
  27. >long int count;
  28. >
  29. >int
  30. >main (void)
  31. >{
  32. >    do
  33. >    {
  34. >        (l = i + j + k);
  35. >        (i = 1);
  36. >        (j = 2);
  37. >        (k = 3);
  38. If you are initializing i, j and k every time in the loop, you will get the
  39. same result in each line.
  40.  
  41. >        (i++, j++, k++);
  42. >        (++count);
  43. >    if (count % 1000000 == 0)
  44. >     {
  45. >        printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
  46. A '\n' in the format would be nice:                     ^^
  47.         printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)\n", ...
  48. >     }
  49. >
  50. >    } while (0 < i < j < k < l);
  51. You have to split this statement:
  52. while(0<i && i<j && j<k && k<l);
  53. Otherwise the following will be done:
  54. Assuming k<l, then it results 1 and next you will compare j<1 which is false
  55. and your loop finishs.
  56. >
  57. >    return (0);
  58. >}
  59. I can't see where you are checking your condition (i+j+k) == l and how
  60. to evaluate l. At present the value of l is zero because i, j and k are
  61. global and therefore initialized to zero.
  62.  
  63. Hope this will help you,
  64. Matthias
  65.  
  66.